/**
 * 
 */
package japavo.gui.general_ui;

import japavo.gui.MainFrame;
import japavo.tools.vocabulary.Vocabulary;
import japavo.tools.vocabulary.Vocabulary.ShownWord;
import japavo.tools.vocabulary.VocabularyBook.ShownVocabulary;
import japavo.trainer.VocabularyTrainer;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.util.Observable;
import java.util.Observer;

import javax.swing.AbstractAction;
import javax.swing.ActionMap;
import javax.swing.GroupLayout;
import javax.swing.InputMap;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.KeyStroke;

/**
 *
 */
@SuppressWarnings("serial")
public class TrainingPanel extends JPanel implements Observer {
	/************************************************************************************
	 ******************************* DECLARATIONS *************************************** 
	 ***********************************************************************************/
	private MainFrame mainFrame;
	private VocabularyTrainer trainer;
	private ShownVocabulary currentVocabulary;
	
	// GUI components
	private JLabel questionLabel;
	private JLabel resultLabel;
	private JTextField inputField;
	private JButton checkButton;
	private JButton nextButton;
	
	
	/************************************************************************************
	 ******************************* CONSTRUCTORS *************************************** 
	 ***********************************************************************************/
	/**
	 * 
	 */
	public TrainingPanel(MainFrame mainFrame, VocabularyTrainer trainer) {
		this.mainFrame = mainFrame;
		this.trainer = trainer;
		
		InputMap inputMap = getInputMap(WHEN_IN_FOCUSED_WINDOW);
		ActionMap actionMap = getActionMap();
		
		// components
		inputField = new JTextField();
		questionLabel = new JLabel("Hier kommt was rein");
		resultLabel = new JLabel();
		
		checkButton = new JButton("Überprüfen");
		AbstractAction checkAction = new AbstractAction() {
			@Override
			public void actionPerformed(ActionEvent e) {
				checkInput();
			}
		};
		checkButton.addActionListener(checkAction);
		inputMap.put(KeyStroke.getKeyStroke(System.getProperty("HOTKEY_CHECK")), "check");
		actionMap.put("check", checkAction);
		
		nextButton = new JButton("Weiter");
		
				
		AbstractAction nextAction = new AbstractAction() {
			@Override
			public void actionPerformed(ActionEvent e) {
				TrainingPanel.this.trainer.getNextVocabulary();
			}
		};
		nextButton.addActionListener(nextAction);
		inputMap.put(KeyStroke.getKeyStroke(System.getProperty("HOTKEY_NEXT")), "next");
		actionMap.put("next", nextAction);
		
		// ========== Layout ==========
		GroupLayout layout = new GroupLayout(this);
		setLayout(layout);
		
		// horizontal
		layout.setHorizontalGroup(layout.createSequentialGroup()
				.addGroup(layout.createParallelGroup()
					.addGroup(layout.createSequentialGroup()
						.addComponent(questionLabel)
						.addComponent(inputField)
						.addComponent(resultLabel)
					)
					.addGroup(layout.createSequentialGroup()
						.addComponent(checkButton)
						.addComponent(nextButton)
					)
				)
			);
		
		// vertical		
		layout.setVerticalGroup(layout.createSequentialGroup()
			.addGroup(layout.createParallelGroup()
				.addComponent(questionLabel)
				.addComponent(inputField)
				.addComponent(resultLabel)
			)
			.addGroup(layout.createParallelGroup()
				.addComponent(checkButton)
				.addComponent(nextButton)
			)
			
		);
		
		setVisible(true);
	}

	/************************************************************************************
	 ********************************* FUNCTIONS **************************************** 
	 ***********************************************************************************/
	private void resetTexts() {
		questionLabel.setText(currentVocabulary.vocabulary.getWord(currentVocabulary.shownAs));
		inputField.setText("");
		resultLabel.setText("");
		inputField.setBackground(Color.WHITE);
	}
	
	private void checkInput() {
		Vocabulary vocab = currentVocabulary.vocabulary;
		ShownWord  shown = currentVocabulary.shownAs;
		boolean check = vocab.checkCorrectness(inputField.getText(), shown);
		setResult(check);
		trainer.vocabularyPlayed(check);
	}
	
	private void setResult(boolean correct) {
		inputField.setBackground(correct ? Color.GREEN : Color.RED);
		resultLabel.setText(correct ? "Richtig!" : currentVocabulary.vocabulary.getCorrectWord(currentVocabulary.shownAs));
	}
	
	/************************************************************************************
	 ********************************* INHERITED **************************************** 
	 ***********************************************************************************/
	@Override
	public void update(Observable o, Object arg) {
		if (!(arg instanceof ShownVocabulary))
			return;
		
		currentVocabulary = (ShownVocabulary) arg;
		resetTexts();
	}	
}
